/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_q_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_float:
	    r0.v.i = v->v.d != mpq_get_d(r0.v.mpq);
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) != 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = !mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_q_lt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 < mpq_sgn(r0.v.mpq);
	    break;
	case t_int:
	    r0.v.i = empq_cmp_i(r0.v.mpq, v->v.i) > 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d < mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    mpq_set_z(thr_q0, v->v.mpz);
	    r0.v.i = mpq_cmp(thr_q0, r0.v.mpq) < 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) < 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = mpfr_less_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_q_le(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 <= mpq_sgn(r0.v.mpq);
	    break;
	case t_int:
	    r0.v.i = empq_cmp_i(r0.v.mpq, v->v.i) >= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d <= mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    mpq_set_z(thr_q0, v->v.mpz);
	    r0.v.i = mpq_cmp(thr_q0, r0.v.mpq) <= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) <= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_q_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_float:
	    r0.v.i = v->v.d == mpq_get_d(r0.v.mpq);
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) == 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_q_ge(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 >= mpq_sgn(r0.v.mpq);
	    break;
	case t_int:
	    r0.v.i = empq_cmp_i(r0.v.mpq, v->v.i) <= 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d >= mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    mpq_set_z(thr_q0, v->v.mpz);
	    r0.v.i = mpq_cmp(thr_q0, r0.v.mpq) >= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) >= 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_q_gt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 > mpq_sgn(r0.v.mpq);
	    break;
	case t_int:
	    r0.v.i = empq_cmp_i(r0.v.mpq, v->v.i) < 0;
	    break;
	case t_float:
	    r0.v.i = v->v.d > mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    mpq_set_z(thr_q0, v->v.mpz);
	    r0.v.i = mpq_cmp(thr_q0, r0.v.mpq) > 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = mpq_cmp(v->v.mpq, r0.v.mpq) > 0;
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_rr0, r0.v.mpq, thr_rnd);
	    r0.v.i = mpfr_greater_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_q_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    switch (eq_add_i_q(thr_qr, v->v.i, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d + mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_add_z_q(thr_qr, v->v.mpz, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_add_q_q(thr_qr, v->v.mpq, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_add_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd + mpq_get_d(r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_add_qq_q(thr_qq, v->v.cqq, r0.v.mpq);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
     }
}

void
evm_q_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    mpq_neg(thr_qr, r0.v.mpq);
	    chk_rq();
	    break;
	case t_int:
	    switch (eq_sub_i_q(thr_qr, v->v.i, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d - mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_sub_z_q(thr_qr, v->v.mpz, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_sub_q_q(thr_qr, v->v.mpq, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_sub_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd - mpq_get_d(r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_sub_qq_q(thr_qq, v->v.cqq, r0.v.mpq);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
     }
}

void
evm_q_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    switch (eq_mul_i_q(thr_qr, v->v.i, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d * mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_mul_z_q(thr_qr, v->v.mpz, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_mul_q_q(thr_qr, v->v.mpq, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_mul_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd * mpq_get_d(r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_mul_qq_q(thr_qq, v->v.cqq, r0.v.mpq);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    switch (eq_div_i_q(thr_qr, v->v.i, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d / mpq_get_d(r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_div_z_q(thr_qr, v->v.mpz, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_div_q_q(thr_qr, v->v.mpq, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd / mpq_get_d(r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_div_qq_q(thr_qq, v->v.cqq, r0.v.mpq);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_i_q(thr_zr, v->v.i, r0.v.mpq)) == MININT)
		set_rz();
	    break;
	case t_float:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_d(thr_zr, v->v.d /
				     mpq_get_d(r0.v.mpq))) == MININT)
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_z_q(thr_zr, v->v.mpz, r0.v.mpq)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_q_q(thr_zr, v->v.mpq, r0.v.mpq)) == MININT)
		set_rz();
	    break;
	case t_mpr:		case t_shr:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_r_q(thr_zr, v->v.mpr, r0.v.mpq)) == MININT)
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_trunc_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    switch (eq_rem_i_q(thr_qr, v->v.i, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = fmod(v->v.d, mpq_get_d(r0.v.mpq));
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_rem_z_q(thr_qr, v->v.mpz, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_rem_q_q(thr_qr, v->v.mpq, r0.v.mpq)) {
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    chk_rq();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_rem_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_d_q(0.0, r0.v.mpq);
	    }
	    else {
		er_atan2_i_q(thr_rr, 0, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_i_q(v->v.i, r0.v.mpq);
	    }
	    else {
		er_atan2_i_q(thr_rr, v->v.i, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_atan2_d_q(v->v.d, r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_z_q(v->v.mpz, r0.v.mpq);
	    }
	    else {
		er_atan2_z_q(thr_rr, v->v.mpz, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_q_q(v->v.mpq, r0.v.mpq);
	    }
	    else {
		er_atan2_q_q(thr_rr, v->v.mpq, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_dd_q(v->v.cdd, r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_qq_q(v->v.cqq, r0.v.mpq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_qq_q(thr_c, v->v.cqq, r0.v.mpq);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_d_q(0.0, r0.v.mpq);
	    }
	    else {
		er_pow_i_q(thr_rr, 0, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		if (v->v.i < 0) {
		    r0.t = t_cdd;
		    r0.v.cdd = edd_pow_i_q(v->v.i, r0.v.mpq);
		    chk_rdd();
		}
		else {
		    r0.t = t_float;
		    r0.v.d = ed_pow_i_q(v->v.i, r0.v.mpq);
		}
	    }
	    else if (v->v.i < 0) {
		ecc_pow_i_q(thr_c, v->v.i, r0.v.mpq);
		set_chk_rrr();
	    }
	    else {
		er_pow_i_q(thr_rr, v->v.i, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_float:
	    if (finite(v->v.d) && v->v.d < 0.0) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_d_q(v->v.d, r0.v.mpq);
		chk_rdd();
	    }
	    else {
		r0.t = t_float;
		r0.v.d = ed_pow_d_q(v->v.d, r0.v.mpq);
	    }
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		if (mpz_sgn(v->v.mpz) < 0) {
		    r0.t = t_cdd;
		    r0.v.cdd = edd_pow_z_q(v->v.mpz, r0.v.mpq);
		    chk_rdd();
		}
		else {
		    r0.t = t_float;
		    r0.v.d = ed_pow_z_q(v->v.mpz, r0.v.mpq);
		}
	    }
	    else if (mpz_sgn(v->v.mpz) < 0) {
		ecc_pow_z_q(thr_c, v->v.mpz, r0.v.mpq);
		set_chk_rrr();
	    }
	    else {
		er_pow_z_q(thr_rr, v->v.mpz, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		if (mpq_sgn(v->v.mpq) < 0) {
		    r0.t = t_cdd;
		    r0.v.cdd = edd_pow_q_q(v->v.mpq, r0.v.mpq);
		    chk_rdd();
		}
		else {
		    r0.t = t_float;
		    r0.v.d = ed_pow_q_q(v->v.mpq, r0.v.mpq);
		}
	    }
	    else if (mpq_sgn(v->v.mpq) < 0) {
		ecc_pow_q_q(thr_c, v->v.mpq, r0.v.mpq);
		set_chk_rrr();
	    }
	    else {
		er_pow_q_q(thr_rr, v->v.mpq, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    if (mpfr_number_p(v->v.mpr) && mpfr_sgn(v->v.mpr) < 0) {
		ecc_pow_r_q(thr_c, v->v.mpr, r0.v.mpq);
		set_chk_rrr();
	    }
	    else {
		er_pow_r_q(thr_rr, v->v.mpr, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_pow_dd_q(v->v.cdd, r0.v.mpq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_qq_q(v->v.cqq, r0.v.mpq);
		chk_rdd();
	    }
	    else {
		ecc_pow_qq_q(thr_c, v->v.cqq, r0.v.mpq);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_q(thr_c, v->v.mpc, r0.v.mpq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_q(r0.v.mpq);
	    }
	    else {
		er_abs_q(thr_rr, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_i_q(v->v.i, r0.v.mpq);
	    }
	    else {
		er_hypot_i_q(thr_rr, v->v.i, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_d_q(v->v.d, r0.v.mpq);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_z_q(v->v.mpz, r0.v.mpq);
	    }
	    else {
		er_hypot_z_q(thr_rr, v->v.mpz, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_q_q(v->v.mpq, r0.v.mpq);
	    }
	    else {
		er_hypot_q_q(thr_rr, v->v.mpq, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_q(thr_rr, v->v.mpr, r0.v.mpq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_dd_q(v->v.cdd, r0.v.mpq);
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_qq_q(v->v.cqq, r0.v.mpq);
	    }
	    else {
		er_hypot_qq_q(thr_rr, v->v.cqq, r0.v.mpq);
		set_rr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_q(thr_rr, v->v.mpc, r0.v.mpq);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_mpq(v->v.o, r0.v.mpq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_q_complex(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    mpq_set(thr_qi, r0.v.mpq);
	    mpq_set_ui(thr_qr, 0, 1);
	    set_rqq();
	    break;
	case t_int:
	    mpq_set(thr_qi, r0.v.mpq);
	    empq_set_i(thr_qr, v->v.i);
	    set_rqq();
	    break;
	case t_float:
	    r0.t = t_cdd;
	    imag(r0.v.cdd) = mpq_get_d(r0.v.mpq);
	    real(r0.v.cdd) = v->v.d;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    mpq_set(thr_qi, r0.v.mpq);
	    mpq_set_z(thr_qr, v->v.mpz);
	    set_rqq();
	    break;
	case t_mpq:		case t_shq:
	    mpq_set(thr_qi, r0.v.mpq);
	    mpq_set(thr_qr, v->v.mpq);
	    set_rqq();
	    break;
	case t_mpr:		case t_shr:
	    mpfr_set_q(thr_ri, r0.v.mpq, thr_rnd);
	    mpfr_set(thr_rr, v->v.mpr, thr_rnd);
	    set_chk_rrr();
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}
